package net.fpeg.kotlin.msa.wordbase.controller

import net.fpeg.kotlin.msa.wordbase.dto.*
import net.fpeg.kotlin.msa.wordbase.service.WordBaseService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/wordBase")
class WordBaseController {
    @Autowired
    lateinit var wordBaseService: WordBaseService

    @GetMapping("/tag/{wordValue}")
    fun fetch(
            @RequestHeader("USER_ID") uid: Long,
            @PathVariable("wordValue") wordValue: String
    ) =
            WordTagDto(
                    wordBaseService.find(uid, wordValue).map {
                        WordTagIndexDto(it.wordTag.value)
                    }
            )


    /**
     * 添加单词记录
     */
    @PostMapping("/{wordValue}/{tagValue}")
    @ResponseStatus(code = HttpStatus.CREATED)
    fun add(
            @RequestHeader("USER_ID") uid: Long,
            @PathVariable("wordValue") wordValue: String,
            @PathVariable("tagValue") tagValue: String
    ) = wordBaseService.add(uid, wordValue, tagValue, true).id

    @PutMapping("/tag/{wordValue}/{oldTagValue}/{newTagValue}")
    fun edit(
            @RequestHeader("USER_ID") id: Long,
            @PathVariable("wordValue") wordValue: String,
            @PathVariable("oldTagValue") oldTagValue: String,
            @PathVariable("newTagValue") newTagValue: String
    ) {
        wordBaseService.edit(id, wordValue, oldTagValue, newTagValue)
    }

    /**
     * 删除单词记录
     */
    @DeleteMapping("/{wordValue}")
    @ResponseStatus(code = HttpStatus.NO_CONTENT)
    fun remove(
            @RequestHeader("USER_ID") uid: Long,
            @PathVariable("wordValue") wordValue: String
    ) {
        wordBaseService.remove(uid, wordValue)
    }

    @DeleteMapping("/tag/{wordValue}/{tagValue}")
    @ResponseStatus(code = HttpStatus.NO_CONTENT)
    fun remove(
            @RequestHeader("USER_ID") uid: Long,
            @PathVariable("wordValue") wordValue: String,
            @PathVariable("tagValue") tagValue: String
    ) {
        wordBaseService.remove(uid, wordValue, tagValue)
    }

//    /**
//     * 获取单词详细信息
//     */
//    @GetMapping("/{wordValue}")
//    fun getInfo(
//            @RequestHeader("USER_ID") uid: Long,
//            @PathVariable("wordValue") wordValue: String
//    ) = wordBaseService.getInfo(uid, wordValue).run {
//        WordInfoDto(
//                wordValue,
//                wordNote.run { WordNoteDto(id ?: throw Exception("未知错误"), value) },
//                WordTagSelectDto(wordTags.map { it.run { WordTagSelectIndexDto(value) } })
//        )
//    }


}